﻿using System;
using System.Collections.Generic;
using DosTexPrj.Core.Models;
using FluentNHibernate.Testing;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

namespace DosTexPrj.Implement.NHibernate.Test.Mapping
{
    [TestFixture]
    public class TestMapping
    {
        protected ISessionFactory _sessionFactory;
        protected ISession _session;

        [TestFixtureSetUp]
        public void TestInitial()
        {
            Configuration cfg = NHibernateUtility.BuildConfigurationForSQLite();
            this._sessionFactory = cfg.BuildSessionFactory();
            this._session = this._sessionFactory.OpenSession();
            new SchemaExport(cfg).Execute(false, true, false, this._session.Connection, null);
        }

        [TestFixtureTearDown]
        public void TestFinish()
        {
            this._session.Close();
            this._session.Dispose();
            this._sessionFactory.Dispose();
        }

        [Test]
        public void Can_Mapping_RelationShip_Role_User()
        {
            var users = new List<User>();
            users.Add(new User
                                    {
                                        FirstName = "Kim",
                                        LastName = "Cu Hoang",
                                        Username = "kimcu",
                                        Password = "password"
                                    });
            new PersistenceSpecification<Role>(this._session)
                .CheckProperty(c => c.Id, 1)
                .CheckProperty(c => c.RoleName, "Administrator")
                .CheckList(c => c.UsersOfRole, users, (c, a) => c.UsersOfRole.Add(a))
                .VerifyTheMappings();
        }

        [Test]
        public void Can_Mapping_RelationShip_User_Role()
        {
            var role = new Role { RoleName = "Administrator" };
            var user = new User
            {
                FirstName = "Kim",
                LastName = "Cu Hoang",
                Username = "kimcu",
                Password = "password",
            };
            new PersistenceSpecification<User>(this._session)
                            .CheckProperty(u => u.FirstName, "Kim")
                            .CheckProperty(u => u.LastName, "Kim")
                            .CheckProperty(u => u.Username, "Kim")
                            .CheckProperty(u => u.Password, "Kim")
                            .CheckProperty(u => u.Id, Guid.NewGuid())
                            .CheckReference(u => u.Role, role);
        }
    }
}